package com.lvmama.pangolin.controller;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.reflect.Field;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageInfo;
import com.lvmama.pangolin.common.ExportCSVUtils;
import com.lvmama.pangolin.common.StringTools;
import com.lvmama.pangolin.entity.TicketCommodityGrade;
import com.lvmama.pangolin.entity.TicketCommodityGradeVO;
import com.lvmama.pangolin.entity.TicketProductGrade;
import com.lvmama.pangolin.service.TicketCommodityGradeService;
import com.lvmama.pangolin.service.TicketProductGradeService;

@Controller
@RequestMapping("/ticketReports")
public class TicketReportController {

	private Logger logger = Logger.getLogger(getClass());

	@Autowired
	private TicketProductGradeService ticketProductGradeService;
	@Autowired
	private TicketCommodityGradeService ticketCommodityGradeService;

	@Value("#{'${default.pageSize}'}")
	private String pageSize;

	@RequestMapping("/product/show")
	public String ticketProductGradeShow(ModelMap model, TicketProductGrade ticketProductGrade, String page,
			HttpServletRequest request) {

		int size = Integer.valueOf(pageSize);
		String pageNum = request.getParameter("page");
		pageNum = StringTools.isValid(pageNum) ? pageNum : "0";

		PageInfo<TicketProductGrade> pageInfo = ticketProductGradeService.queryByPage(ticketProductGrade,
				Integer.valueOf(pageNum), size);
		model.addAttribute("resultList", pageInfo.getList());
		model.addAttribute("page", pageInfo);
		model.addAttribute("ticketProductGrade", ticketProductGrade);

		return "/ticketReports/product";
	}

	@RequestMapping("/product/export")
	public void exportTicketProduct(TicketProductGrade ticketProductGrade, HttpServletResponse response,
			HttpServletRequest request) throws IOException {

		List<TicketProductGrade> ticketProductList = ticketProductGradeService.query(ticketProductGrade);
		// 生成提示信息，
		response.setContentType("application/octet-stream");
		// 进行转码，使其支持中文文件名
		String codedFileName = java.net.URLEncoder.encode(String.format("%s产品得分", ticketProductGrade.getCompanyCode()),
				"UTF-8");
		response.setHeader("Content-Disposition", "attachment; filename=" + codedFileName + ".csv");
		BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), "gbk"), 1024);

		String[] headers = { "产品ID", "监控景区", "省份", "价格得分", "促销得分", "预定时间得分", "生效时间得分", "退改得分", "商品得分", "SKU", "总得分" };

		Field[] fields = getTicketProductFields(headers);
		ExportCSVUtils<TicketProductGrade> exportCSVUtils = new ExportCSVUtils<TicketProductGrade>();
		try {
			logger.info("门票产品得分导出数据条数:" + ticketProductList.size() + "条");
			exportCSVUtils.exportCSV(headers, fields, ticketProductList, bf, "yyyy-MM-dd HH:mm:ss");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/commodity/show")
	public String ticketCommodityGradeShow(ModelMap model, TicketCommodityGrade ticketCommodityGrade, String page,
			HttpServletRequest request) {

		int size = Integer.valueOf(pageSize);
		String pageNum = request.getParameter("page");
		pageNum = StringTools.isValid(pageNum) ? pageNum : "0";

		PageInfo<TicketCommodityGradeVO> pageInfo = ticketCommodityGradeService.queryByPage(ticketCommodityGrade,
				Integer.valueOf(pageNum), size);
		model.addAttribute("resultList", pageInfo.getList());
		model.addAttribute("page", pageInfo);
		model.addAttribute("ticketCommodityGrade", ticketCommodityGrade);

		return "/ticketReports/commodity";
	}

	@RequestMapping("/commodity/export")
	public void exportTicketCommodity(TicketCommodityGrade ticketCommodityGrade, HttpServletResponse response,
			HttpServletRequest request) throws IOException {

		List<TicketCommodityGradeVO> ticketCommodityList = ticketCommodityGradeService.query(ticketCommodityGrade);
		// 生成提示信息，
		response.setContentType("application/octet-stream");
		// 进行转码，使其支持中文文件名
		String codedFileName = java.net.URLEncoder
				.encode(String.format("%s明细反馈表", ticketCommodityGrade.getCompanyCode()), "UTF-8");
		response.setHeader("Content-Disposition", "attachment; filename=" + codedFileName + ".csv");
		BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), "gbk"), 1024);

		String[] headers = { "监控平台", "商品ID", "商品名称", "驴妈妈对应商品ID", "驴妈妈对应产品ID", "驴妈妈产品名称", "省份", "城市", "价格", "驴妈妈价格",
				"价格得分", "驴妈妈价格得分", "促销金额", "驴妈妈促销金额", "促销得分", "驴妈妈促销得分", "提前预定时间", "驴妈妈提前预定时间", "提前预定时间得分",
				"驴妈妈提前预定时间得分", "退改政策", "驴妈妈退改政策", "退改得分", "驴妈妈退改得分" };

		Field[] fields = getTicketCommodityFields(headers);
		ExportCSVUtils<TicketCommodityGradeVO> exportCSVUtils = new ExportCSVUtils<TicketCommodityGradeVO>();
		try {
			logger.info("门票商品得分导出数据条数:" + ticketCommodityList.size() + "条");
			exportCSVUtils.exportCSV(headers, fields, ticketCommodityList, bf, "yyyy-MM-dd HH:mm:ss");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Field[] getTicketCommodityFields(String[] headers) {
		Field[] fields = new Field[headers.length];
		TicketCommodityGradeVO t = new TicketCommodityGradeVO();
		try {
			for (int i = 0; i < headers.length; i++) {
				if (headers[i].equals("监控平台")) {
					fields[i] = t.getClass().getDeclaredField("monitorPlatform");
				} else if (headers[i].equals("商品ID")) {
					fields[i] = t.getClass().getDeclaredField("commodityId");
				} else if (headers[i].equals("商品名称")) {
					fields[i] = t.getClass().getDeclaredField("commodityName");
				} else if (headers[i].equals("驴妈妈对应商品ID")) {
					fields[i] = t.getClass().getDeclaredField("lmmCommodityId");
				} else if (headers[i].equals("驴妈妈对应产品ID")) {
					fields[i] = t.getClass().getDeclaredField("lmmProductId");
				} else if (headers[i].equals("驴妈妈产品名称")) {
					fields[i] = t.getClass().getDeclaredField("lmmSceneryName");
				} else if (headers[i].equals("省份")) {
					fields[i] = t.getClass().getDeclaredField("provinceName");
				} else if (headers[i].equals("城市")) {
					fields[i] = t.getClass().getDeclaredField("cityName");
				} else if (headers[i].equals("价格")) {
					fields[i] = t.getClass().getDeclaredField("salePrice");
				} else if (headers[i].equals("驴妈妈价格")) {
					fields[i] = t.getClass().getDeclaredField("lmmSalePrice");
				} else if (headers[i].equals("价格得分")) {
					fields[i] = t.getClass().getDeclaredField("salePriceGrade");
				} else if (headers[i].equals("驴妈妈价格得分")) {
					fields[i] = t.getClass().getDeclaredField("lmmSalePriceGrade");
				} else if (headers[i].equals("促销金额")) {
					fields[i] = t.getClass().getDeclaredField("preferentialValue");
				} else if (headers[i].equals("驴妈妈促销金额")) {
					fields[i] = t.getClass().getDeclaredField("lmmPreferentialValue");
				} else if (headers[i].equals("促销得分")) {
					fields[i] = t.getClass().getDeclaredField("preferentialGrade");
				} else if (headers[i].equals("驴妈妈促销得分")) {
					fields[i] = t.getClass().getDeclaredField("lmmPreferentialGrade");
				} else if (headers[i].equals("提前预定时间")) {
					fields[i] = t.getClass().getDeclaredField("bookTime");
				} else if (headers[i].equals("驴妈妈提前预定时间")) {
					fields[i] = t.getClass().getDeclaredField("lmmBookTime");
				} else if (headers[i].equals("提前预定时间得分")) {
					fields[i] = t.getClass().getDeclaredField("bookTimeGrade");
				} else if (headers[i].equals("驴妈妈提前预定时间得分")) {
					fields[i] = t.getClass().getDeclaredField("lmmBookTimeGrade");
				} else if (headers[i].equals("退改政策")) {
					fields[i] = t.getClass().getDeclaredField("refundCond");
				} else if (headers[i].equals("驴妈妈退改政策")) {
					fields[i] = t.getClass().getDeclaredField("lmmRefundCond");
				} else if (headers[i].equals("退改得分")) {
					fields[i] = t.getClass().getDeclaredField("refundCondGrade");
				} else if (headers[i].equals("驴妈妈退改得分")) {
					fields[i] = t.getClass().getDeclaredField("lmmRefundCondGrade");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fields;
	}

	public Field[] getTicketProductFields(String[] headers) {
		Field[] fields = new Field[headers.length];
		TicketProductGrade t = new TicketProductGrade();
		try {
			for (int i = 0; i < headers.length; i++) {
				if (headers[i].equals("产品ID")) {
					fields[i] = t.getClass().getDeclaredField("productId");
				} else if (headers[i].equals("监控景区")) {
					fields[i] = t.getClass().getDeclaredField("sceneryName");
				} else if (headers[i].equals("省份")) {
					fields[i] = t.getClass().getDeclaredField("provinceName");
				} else if (headers[i].equals("价格得分")) {
					fields[i] = t.getClass().getDeclaredField("priceGrade");
				} else if (headers[i].equals("促销得分")) {
					fields[i] = t.getClass().getDeclaredField("preferentialGrade");
				} else if (headers[i].equals("预定时间得分")) {
					fields[i] = t.getClass().getDeclaredField("booktimeGrade");
				} else if (headers[i].equals("生效时间得分")) {
					fields[i] = t.getClass().getDeclaredField("efficientGrade");
				} else if (headers[i].equals("退改得分")) {
					fields[i] = t.getClass().getDeclaredField("refundCondGrade");
				} else if (headers[i].equals("商品得分")) {
					fields[i] = t.getClass().getDeclaredField("commodityGrade");
				} else if (headers[i].equals("SKU")) {
					fields[i] = t.getClass().getDeclaredField("skuGrade");
				} else if (headers[i].equals("总得分")) {
					fields[i] = t.getClass().getDeclaredField("totalGrade");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fields;
	}

}
